const request = require('request');

 class httpClien{
  async get(url, tryTimes=1) {
    return new Promise((resolve, reject) => {
      const options = {
        url: url,
        timeout:15000,
        json:true,
        method: 'GET'
    };

  
    let ths = this;
    return request(options, async function(error, response, body) {
        if (error || response.statusCode != 200) {
          if((error.code == 'ESOCKETTIMEDOUT' || error.Error == 'ETIMEDOUT') && tryTimes < 4) {  //如果超时
            console.log(`超时15s，正在重新发起第${tryTimes+1}次请求...`)
            tryTimes++;
            return await ths.get(url, tryTimes);
          }
          if(error.code == 'ESOCKETTIMEDOUT' || error.Error == 'ETIMEDOUT') {
            console.log(`请求超时！！请求链接：${url}`)
          }

          return resolve(false);
        }
  
        return resolve({
          data: body
        })
      })
    })
  }
}


module.exports  = httpClien;
